﻿--[[
   @brief    social network utility
   @author   vincentlao
   @version  1.0
   @date     2015-12-17
--]]

social_utility = 
{
    MSG_TYPE_LAUNCH_TEAM_INVITE = 1;
    MSG_TYPE_SHARE_TEAM_INVITE = 2;
    MSG_TYPE_PERSON_INFO_CARD = 3;
    MSG_TYPE_LOL_KAIHEI_INVITE = 4;

    format_discussion_msg = function(message)
        --如果群是private的修改群标题, 修改消息text字段
        if message.msg_type == 1 then 
            local g_info = group_list_model.get_group_info(message.group_id);
            if g_info ~= nil and g_info.group_type == "MobileDiscus" then
                local input = {list_id = {message.sender}};
                local output = comm_center.call_service('i_service_social_info_center_get_user_info', input);
                local friend_info = output['list_user_info'][1];

                if message.action_type == 0x259 then
                    return friend_info.name..LoadStr("SOCIAL_TALK_DIALOG_MODIFY_ROOM_SUBJECT")..message.message;
                elseif message.action_type == 0x01 then
                    local param = base_utility.json_to_table(message.message);
                    --测试过程中出现了param 为 nil 的情况，这里保护下
                    if param == nil then
                        base_utility.log("[format_discussion_msg] error: "..base_utility.table_to_json(message), true);
                        return nil;
                    end
                    local msg_text = friend_info.name..LoadStr("SOCIAL_TALK_DIALOG_INVITE");
                    for i, f_id in ipairs(param.invited_list) do
                        local input = {list_id = {f_id}};
                        local output = comm_center.call_service('i_service_social_info_center_get_user_info', input);
                        local f_info = output['list_user_info'][1];
                        msg_text = msg_text..f_info.name.." ";
                    end
                    msg_text = msg_text..LoadStr("SOCIAL_TALK_DIALOG_JOIN_ROOM");
                    return msg_text;
                end
            end          
        end
        return nil;
    end;

    format_msg = function(message)
        if message == nil then
            base_utility.log("Error! format msg is error!", true);
            return "";
        end
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        local msg = string.gsub(message, "%[br%]", " ");

        local formated_msg_t = ierd_tgp.social_network.tagFormatedMsg:new();
        social_network:format_msg(msg, formated_msg_t);
        local msg_text = formated_msg_t.msg_fromated;
        if string.sub(msg_text, 1, 1) == "{" and string.sub(msg_text, -1, -1) == "}"then
            local msg_json = base_utility.json_to_table(msg_text);
            if msg_json ~= nil then
                if msg_json.text ~= nil then
                    msg_text  = msg_json.text;
                elseif msg_json.image_detail_info_list ~= nil then
                    msg_text = "[" .. LoadStr("PWG_SOCIAL_IMAGE_MSG") .. "]";
                elseif msg_json.action == 'gameinvite' then
                    msg_text = "[" .. LoadStr("PWG_SOCIAL_INVITE_MSG") .. "]";
                elseif msg_json.gameid ~= nil and 
                       msg_json.fromid  ~= nil and 
                       msg_json.toid ~= nil then
                    msg_text = "[" .. LoadStr("PWG_SOCIAL_GIFT_MSG") .. "]";
                elseif msg_json.ref_msg ~= nil and
                       msg_json.reply_content ~= nil then
                    msg_text = "[" .. LoadStr("PWG_SOCIAL_REF_MSG") .. "]" .. msg_json.reply_content;
                end

                if msg_json.type ~= 0 then
                    if msg_json.type == social_utility.MSG_TYPE_LAUNCH_TEAM_INVITE then
                        msg_text = LoadStr("SOCIAL_TALK_DIALOG_SEND_TEAM_INVITE");
                        if msg_json.options ~= nil and msg_json.options.owner ~= nil and msg_json.options.owner.tgp_name ~= nil then
                            msg_text = string.format("%s %s", msg_json.options.owner.tgp_name, msg_text); 
                        end
                    elseif msg_json.type == social_utility.MSG_TYPE_SHARE_TEAM_INVITE then
                        msg_text = LoadStr("SOCIAL_TALK_DIALOG_SHARE_TEAM_INVITE");
                        if msg_json.data ~= nil and msg_json.data.sharerName ~= nil then
                            msg_text = string.format("%s %s", msg_json.options.sharerName, msg_text); 
                        end  
                    elseif msg_json.type == social_utility.MSG_TYPE_PERSON_INFO_CARD then
                        msg_text = LoadStr("SOCIAL_TALK_DIALOG_SEND_PERSON_CARD");
                        if msg_json.data ~= nil and msg_json.data ~= nil and msg_json.data.tgpname ~= nil then
                            msg_text = string.format("%s%s", msg_json.data.tgpname, msg_text); 
                        end
                    elseif msg_json.type == social_utility.MSG_TYPE_LOL_KAIHEI_INVITE then
                         msg_text = LoadStr("SOCIAL_TALK_DIALOG_SEND_KAIHEI_INVITE");                
                         if msg_json.options ~= nil and msg_json.options.owner ~= nil and msg_json.options.owner.tgp_name ~= nil then
                            msg_text = string.format("[%s] %s", msg_json.options.owner.tgp_name, msg_text); 
                        end
                    end
                end
            end    
        end

        formated_msg_t:delete();
        
        return msg_text;
    end;

    is_my_friend = function(friend_id)
        local is_friend = false;
        local friend_info = friend_list_model.get_friend_info(friend_id);
        if friend_info  ~= nil then
            is_friend = true;
        end;
        return is_friend;
    end;

    is_friend_in_group = function(group_id, friend_id)
        return false;
    end;


    --------------------------------------------------------------------------------
    --开黑相关函数
    get_lol_white_list_info_detail = function(white_list_info, friend_id)
        local my_white_list_info = nil;
        local friend_white_list_info = nil;
        if white_list_info ~= nil and white_list_info.retCode == 0 then
            if tostring(white_list_info.data[1].qquin) == login_model.im_login_infomation["friend_id"] then
                my_white_list_info = white_list_info.data[1];
                friend_white_list_info = white_list_info.data[2];
            else
                friend_white_list_info = white_list_info.data[1];
                my_white_list_info = white_list_info.data[2];
            end
        end

        return my_white_list_info, friend_white_list_info;
    end;

    check_friend_keihei = function(friend_white_list_info, friend_nickname)
        --人数未满，判断对方是否有资格
        base_utility.log("[check_friend_keihei] friend_white_list_info:"..base_utility.table_to_json(friend_white_list_info), true);
        local is_success = true;
        local error_msg = "";
        if friend_white_list_info.errorcode == 0 then
            is_success = true;
        elseif friend_white_list_info.errorcode == 1004 then
            is_success = false;
            error_msg = "\""..friend_nickname.."\""..LoadStr("SOCIAL_UTILITY_NO_ROLE_TO_ACC_INVITE");
        elseif friend_white_list_info.errorcode == 2 then
            is_success = false;
            error_msg = "\""..friend_nickname.."\""..LoadStr("SOCIAL_UTILITY_LT_15_TO_ACC_INVITE");
        elseif friend_white_list_info.errorcode == 3 then
            is_success = false;
            error_msg = "\""..friend_nickname.."\""..LoadStr("SOCIAL_UTILITY_FORBBIDEN_TO_ACC_INVITE");
        else
            is_success = false;
            error_msg = LoadStr("SOCIAL_TALK_DIALOG_NOT_WHITE_LIST_NOT_INVITE");
            base_utility.log("[check_friend_keihei] friend_white_list_info:"..base_utility.table_to_json(friend_white_list_info), true);
        end
        return is_success, error_msg;
    end;

    check_self_kaihei = function(my_white_list_info)
        base_utility.log("[check_self_kaihei] my_white_list_info:"..base_utility.table_to_json(my_white_list_info), true);
        local is_success = true;
        local error_msg = "";
        --判断自己的资格
        if my_white_list_info.errorcode == 0 then
            is_success = true;
        elseif my_white_list_info.errorcode ==  1004 then
            is_success = false;
            error_msg = LoadStr("SOCIAL_TALK_DIALOG_NO_ROLE_NOT_INVITE");
        elseif my_white_list_info.errorcode == 2 then
            is_success = false;
            error_msg = LoadStr("SOCIAL_TALK_DIALOG_NOT_15_LEVEL_NOT_INVITE");
        elseif my_white_list_info.errorcode == 3 then
            is_success = false;
            error_msg = LoadStr("SOCIAL_TALK_DIALOG_FORBIDDEN_NOT_INVITE");
        elseif my_white_list_info.errorcode == 4 then
            is_success = false;
            error_msg = LoadStr("SOCIAL_TALK_DIALOG_UPGRADE_NOT_INVITE");
        else
            is_success = false;
            error_msg = LoadStr("SOCIAL_TALK_DIALOG_NOT_WHITE_LIST_NOT_INVITE");
            base_utility.log("[check_self_kaihei] friend_white_list_info:"..base_utility.table_to_json(my_white_list_info), true);
        end
        return is_success, error_msg;
    end;

    --开黑条件判断，返回错误msg
   -- # 排位赛 TLB_DB_ROOM_MODE_RANK = 1  2人
    --# 娱乐赛 TLB_DB_ROOM_MODE_ENTERTAINMENT = 2 5人
    check_kaihei_condition = function(my_lol_room_info, my_white_list_info, friend_white_list_info, friend_nickname)
        local is_have_room = false;
        local is_success = false;
        local error_msg = "";
        --1.自己是否有房间
        if  my_lol_room_info ~= nil and
            my_lol_room_info.retCode == 0 and
            my_lol_room_info.data ~= nil and
            my_lol_room_info.data.room_id ~= 0 then
            base_utility.log("[check_kaihei_condition] my_lol_room_info:"..base_utility.table_to_json(my_lol_room_info), true);
            is_have_room = true;
            is_success = true;

            --有房间
            --判断房间是否满员
            local max_num = 5;
            if my_lol_room_info.data.room_mode == 1 then -- 娱乐赛5人
                max_num = 2;
            end
            if max_num == my_lol_room_info.data.player_num then
                is_success = false;
                error_msg = LoadStr("SOCIAL_TALK_DIALOG_ROOM_FULL_NOT_INVITE"); 
            else
                is_success, error_msg = social_utility.check_friend_keihei(friend_white_list_info, friend_nickname);
            end

        else
            is_have_room = false;
            is_success, error_msg = social_utility.check_self_kaihei(my_white_list_info);

            --如果自己符合，则坚持对方是否可以被邀请
            if is_success == true then
                is_success, error_msg = social_utility.check_friend_keihei(friend_white_list_info, friend_nickname);
            end
        end 
        
        return is_have_room, is_success, error_msg;
    end;
    ----------------------------------------------------------------------------

    number_format = function (number)
        local str = "0";
        if type(number) == "number" then
            if number > 0 then
                str = "";
            end
            while number > 0 do
                local a = number % 1000;
                number = math.modf(number / 1000);
                if string.len(str) > 0 then
                    str = ","..str;
                end
                if number > 0 then
                    str = string.format("%03d", a)..str;
                else
                    str = tostring(a)..str;
                end
            end
        end
        return str;
    end;

    -- 主窗口是否最小化
    is_main_wnd_minisize = function()
        local is_minisize = true;
        local pMainFrame_Dialog_ = ui_utility.get_ctrl_ptr_by_cid(CID_MAINFRAME_Dialog_MainFrame);
        if pMainFrame_Dialog_ ~= nil then
            local rc_mainframe = pMainFrame_Dialog_:GetRectEx(FALSE,TRUE);
            local height = rc_mainframe.bottom - rc_mainframe.top;
            if height > 0 then
                is_minisize = false;
            end
        end
        return is_minisize;
    end;
}